package com.chat.room.client;

import com.chat.room.ClientRun;
import com.chat.room.bean.ServerInfo;
import com.chat.room.common.ClientConstant;
import com.chat.room.common.CommonConstant;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;

@Slf4j
public class ChatRoomClient {

    private Socket socket;

    private ChatRoomSendMsg chatRoomSendMsg;

    private ChatRoomReceiveMsg chatRoomReceiveMsg;

    public ChatRoomClient(ServerInfo serverInfo) {
        socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(serverInfo.getIp(), serverInfo.getPort()), 3000);
            log.info("服务器连接成功");
        } catch (IOException e) {
            log.error("连接异常：{}", e);
        }
    }

    public void start() {
        chatRoomSendMsg = new ChatRoomSendMsg();
        chatRoomReceiveMsg = new ChatRoomReceiveMsg();
        ClientConstant.THREAD_POOL_EXECUTOR.execute(chatRoomSendMsg);
        ClientConstant.THREAD_POOL_EXECUTOR.execute(chatRoomReceiveMsg);
    }

    public void shutdown() {
        chatRoomSendMsg.close();
        chatRoomReceiveMsg.close();
        if (null != socket && !socket.isConnected()) {
            try {
                socket.close();
            } catch (IOException e) {
                log.error("关闭socket异常：{}", e);
            }
        }
        log.info("服务端连接断开");
        ClientRun.COUNT_DOWN_LATCH.countDown();
    }


    private class ChatRoomSendMsg implements Runnable {

        private boolean flag = true;

        @Override
        public void run() {
            log.info("您可以开始发送消息了");
            try (PrintStream printStream = new PrintStream(socket.getOutputStream());
                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))
            ) {
                while (flag) {
                    String recivie = bufferedReader.readLine();
                    printStream.println(recivie);
                    if (CommonConstant.BYEBYE.equals(recivie)) {
                        shutdown();
                    }
                }
            } catch (IOException e) {
                log.error("消息发送异常：{}", e);
            }
        }

        public void close() {
            this.flag = false;
        }

    }

    private class ChatRoomReceiveMsg implements Runnable {

        private boolean flag = true;

        @Override
        public void run() {
            log.info("您已成功连接到聊天室");
            try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                while (flag) {
                    String message = bufferedReader.readLine();
                    log.info("接收到消息：{}", message);
                }
            } catch (IOException e) {
                log.error("连接关闭");
            }
        }

        public void close() {
            this.flag = false;
        }
    }


}